
import 'package:encrypt/encrypt.dart';
import 'package:pointycastle/asymmetric/api.dart';
import 'package:flutter_syt/utils/utils_dlog.dart';

const String _KEY = 'PJD6Ts7RGpCGnUez';
const String _SM4KEY = '428c9516e8e1ce511c4c4e159b8731aa';
/// Created by XieXin on 2021/8/13.
///
/// 加密工具类
class EncryptUtils {
  ///aes
  // static late Key _keyAes;
  static late Encrypter _encryptAes;
  static IV _ivAes = IV.fromLength(16);

  ///rsa
  static late RSAPublicKey _publicKey;
  // static late RSAPrivateKey _privateKey;
  static late Encrypter _encryptRsa;
  static late Signer _signer;

  /// Rsa加密最大长度
  static const int MAX_ENCRYPT_BLOCK = 117;

  /// Rsa解密最大长度
  static const int MAX_DECRYPT_BLOCK = 128;

  ///初始化AES加密启动时调用
  // static Future<void> initAes(
  //     String key, {
  //       mode = AESMode.sic,
  //       padding = 'PKCS7',
  //     }) async {
  //   if (key.length == 16 || key.length == 24 || key.length == 32) {
  //     _keyAes = Key.fromUtf8(key);
  //     _encryptAes = Encrypter(AES(_keyAes, mode: mode, padding: padding));
  //     return;
  //   }
  //   print("密钥长度为16/24/32位");
  // }
  static var RSAKey = '''-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDiXVa6quq9sj7KqTnNuYg/KKi2
QFcvCkYag6HZZPFX0RrIaAf7PzvIY58KLYnTEEdC8QLu17zHnn/9Up8VmSU1QcXt
sKithx9FcHPjV7u5YOH3IIgaF38sUk+7ZbmdyI46YSN76K78y2Pb93mrjWTLMz3i
MpKh3PNli7gOug4dJwIDAQAB
-----END PUBLIC KEY-----''';

  ///初始化RAS加密启动时调用
  static Future<void> initRsa() async {
    // String publicKeyString = await rootBundle.loadString(publicPath);
    // String privateKeyString = await rootBundle.loadString(privatePath);
    _publicKey = RSAKeyParser().parse(RSAKey) as RSAPublicKey;
    // PrivateKey
    // rsap
    // RSAKeyParser().parse(RSAKey) as RSAPrivateKey;
//  RSAKeyParser().parse(RSAKey) as RSAPublicKey;
    _encryptRsa = Encrypter(RSA(
      publicKey: _publicKey,
      // privateKey: RSAKeyParser().parse(RSAKey) as RSAPrivateKey)
    ));
    _signer = Signer(RSASigner(RSASignDigest.SHA256, publicKey: _publicKey));
  }

  // ///Aes加密
  // static String encryptAes(String context) {
  //   return _encryptAes.encrypt(context, iv: _ivAes).base64;
  // }

  // ///Aes解密
  // static String decryptAes(String context) {
  //   return _encryptAes.decrypt(Encrypted.fromBase64(context), iv: _ivAes);
  // }

  ///公钥Rsa加密
  static String encryptRsa(String context) {
    var en = _encryptRsa.encrypt(context);
    return en.base64;
  }

  ///公钥Rsa解密
  static String decryptRsa(String data) {
    var result = _encryptRsa.decrypt(Encrypted.fromBase64(data));
    return result;
  }

  ///用私钥对信息生成数字签名
  static String sign(String data) {
    return _signer.sign(data).base64;
  }

  ///校验数字签名
  static bool verify(String data, String sign) {
    return _signer.verify64(data, sign);
  }

  ///加密
  static String encryptNewAES(String data) {
    try {
      final key = Key.fromUtf8(_KEY);
      // final iv = IV.fromUtf8(_IV);
      final iv = IV.fromUtf8('');
      final encrypter = Encrypter(AES(key, mode: AESMode.ecb));
      final encrypted = encrypter.encrypt(data, iv: iv);

      return encrypted.base64;
    } catch (err) {
      DLog("aes encode error:$err");
      return '';
    }
  }


  // encrypt

  ///解密
  static decryptNewAES(encrypted) {
    try {
      final key = Key.fromUtf8(_KEY);
      final iv = IV.fromUtf8('');
      final encrypter = Encrypter(AES(
        key,
        mode: AESMode.ecb,
      ));
      final decrypted = encrypter.decrypt64(encrypted, iv: iv);
      return decrypted;
    } catch (err) {
      DLog("aes decode error:$err");
      return encrypted;
    }
  }
}

extension StringExtension on String {
  String get rsa {
    return EncryptUtils.encryptRsa(this);
  }

  String get deRsa {
    return EncryptUtils.decryptRsa(this);
  }



}
